<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Auth;

class Order extends Model
{
    use SoftDeletes; // 使用软删除
    protected $appends = ['status_label']; // 自定义字段
    protected $dates = [
        'created_at',
        'updated_at',
        'paid_at', // 支付时间
        'delivery_at', // 发货时间
        'completed_at', // 完成时间
        'deleted_at'
    ];

    const STATUS_UNCONFIRMED = 'unconfirmed';
    const STATUS_UNPAID = 'unpaid';
    const STATUS_PAID = 'paid';
    const STATUS_DELIVERING = 'delivering';
    const STATUS_COMPLETED = 'completed';
    const STATUS_REFUND = 'refund';
    const STATUS_REFUNDING = 'refunding';
    const STATUS_REFUNDED = 'refunded';
    const STATUS_CANCELLED = 'cancelled';

    public static function boot() {
        parent::boot();
        
        static::creating(function($model) {
            $model->user_id = request()->user()->id;
            $model->order_no = self::orderNoGenerator();
        });
    }

    /**
     * 获得此订单的商品。
     */
    public function orderItems() {
        return $this->hasMany('App\Models\OrderItem');
    }

    /**
     * 获得此订单的用户。
     */
    public function user() {
        return $this->belongsTo('App\Models\User');
    }

    /**
     * 获得此订单的店铺。
     */
    public function shop()
    {
        return $this->belongsTo('App\Models\Shop');
    }
    
    /**
     * 限制查询自己的订单。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOwner($query)
    {
        return $query->where('user_id', Auth()->user()->id);
    }

    /**
     * 限制查询自己店铺的订单。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOwnerShop($query)
    {
        return $query->where('shop_id', Auth::guard('shop')->user()->id);
    }


    /**
     * 订单状态描述
     */
    public function getStatusLabelAttribute(){
        $statusMap = array(
            'unconfirmed' => '等待确认',
            'unpaid' => '未支付',
            'paid' => '已支付',
            'delivering' => '已发货',
            'completed' => '已完成',
            'refund' => '申请退货',
            'refunding' => '退货中',
            'refunded' => '已退货',
            'cancelled' => '已取消',
        );
        return $statusMap[$this->attributes['status']];
    }


    /**
     * 获取订单状态
     */
    static function getOrderStatus($status = null) {
        $statusMap = array(
            'unconfirmed' => self::STATUS_UNCONFIRMED,
            'unpaid' => self::STATUS_UNPAID,  // 未支付
            'paid' => self::STATUS_PAID,    // 已支付
            'delivering' => self::STATUS_DELIVERING, // 已发货
            'completed' => self::STATUS_COMPLETED, // 已完成
            'refund' => self::STATUS_REFUND, // 退货申请
            'refunding' => self::STATUS_REFUNDING, // 退货中
            'refunded' => self::STATUS_REFUNDED, // 已退货
            'cancelled' => self::STATUS_CANCELLED, // 取消交易
        );

        if($status) {
            return $statusMap[$status] !== null ? $statusMap[$status] : $status;
        }else {
            return $statusMap;
        }
    }

    /**
     * 生成订单号
     * 
     * @return String
     */
    protected static function orderNoGenerator()
    {
        $microtime = microtime(true) * 10000;
        $number = date('YmdHis') . rand(111, 999) . substr($microtime, -5);
        $flag = self::where('order_no', $number)->count();
        if ($flag > 0){
            $number = self::orderNoGenerator();
        }
        return $number;
    }
}
